package com.easy.controller;

import com.easy.bean.Equipment;
import com.easy.service.EquipmentService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/api/equipment")
public class EquipmentController {

    @Autowired
    private EquipmentService equipmentService;

    // 新增设备
    @PostMapping
    public Result add(@RequestBody Equipment equipment) {
        return Result.success(equipmentService.add(equipment));
    }

    // 编辑设备
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody Equipment equipment) {
        equipment.setId(id);
        return Result.success(equipmentService.edit(equipment));
    }

    // 删除设备
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success(equipmentService.del(id));
    }

    // 查询所有设备（可模糊查询）
    @GetMapping
    public Result all(
            @RequestParam(name = "name", required = false, defaultValue = "") String name,
            @RequestParam(name = "status", required = false, defaultValue = "") String status
    ) {
        return Result.success(equipmentService.all(name, status));
    }

    // 查询所有设备（别名接口，用于处理/list请求）
    @GetMapping("/list")
    public Result list(
            @RequestParam(name = "name", required = false, defaultValue = "") String name,
            @RequestParam(name = "status", required = false, defaultValue = "") String status
    ) {
        return Result.success(equipmentService.all(name, status));
    }

    // 查询单个设备
    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        return Result.success(equipmentService.one(id));
    }

    // 分页查询设备
    @GetMapping("/page")
    public Result page(Page page, Equipment equipment) {
        equipmentService.page(page, equipment);
        return Result.success(page);
    }
}